package com.ruoyi.project.system.weixinUtil.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.system.weixinUtil.domain.AccessToken;
import com.ruoyi.project.system.weixinUtil.service.CoreService;
import com.ruoyi.project.system.weixinUtil.utils.CommonUtil;
import com.ruoyi.project.system.weixinUtil.utils.SignUtil;
import com.ruoyi.project.system.wxAccesstoken.domain.WxAccesstoken;
import com.ruoyi.project.system.wxAccesstoken.service.IWxAccesstokenService;
import com.ruoyi.project.system.wxAccesstoken.service.WxAccesstokenServiceImpl;
import com.ruoyi.project.system.wxUser.domain.WxUser;
import com.ruoyi.project.system.wxUser.service.IWxUserService;

import net.sf.json.JSONObject;



@Controller
@Component("WeixinJob")
@RequestMapping("/weixinJob")
public class weixinJob {
	@Autowired
	private CoreService coreService;
	@Autowired
	private IWxUserService wxUserService;
	@Autowired
	private IUserService userService;
	
	/**
	 * 发送邀请评价
	 * 查出所有得待评价学员，邀请评价
	 */
	public void sendTemplate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String nowDate = dateFormat.format(new Date());
		User user = null;
		//查出所有得学员（现在是测试，后面得改成查询所有昨天已练过车的学员）
		List<WxUser> list = wxUserService.selectWxUserList(new WxUser());
		if(list.size()>0) {
			for (WxUser wxUser : list) {
				if(wxUser.getUserId()!=null) {
					user = userService.selectUserById(wxUser.getUserId().longValue());
					if(user!=null) {
						Object result = coreService.sendTemplate(user.getUserName(),nowDate,wxUser.getOpenid(),"请你对我们的教练服务做出评价");
						JSONObject jsonObject = JSONObject.fromObject(result);
						System.out.println(jsonObject);
						if(!"".equals(jsonObject)) {
							System.out.println("邀请评价成功");
						}
					}
				}
				
				
			}
		}
		
	}
	
	public void gaoqinmei() {
		coreService.sendTemplate("","2019-08-14","owfSC53jy0JDcyW4B6vM1Ah5wT3c","这周放假咱们出去玩吧");
	}
}
